package edu.kit.aifb.evtcrawl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.log4j.Appender;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

import edu.kit.aifb.evtcrawl.pachube.PachubeRepositoryHandler;
import edu.kit.aifb.evtcrawl.twitter.TwitterRepositoryHandler;
import edu.kit.aifb.evtcrawl.twitter.TwitterHandler;

public class EventApp {

	private static EventApp app;
	private TwitterRepositoryHandler repoHandler;
	private TwitterHandler twitterHandler;
	private String[] keywords;
	
	static Logger log = Logger.getRootLogger();
	static Appender appender;
	
	static boolean crawlTwitter = false;
	static boolean crawlPachube = true;
	
	public static void main(String[] args) {
		app = new EventApp();
		app.keywords = args;
		if(crawlTwitter){
			app.initTwitterCrawl();
			app.runTwitterCrawl();
		}
		else if(crawlPachube){
			app.runPachubeConsole();
		}
	}
	
	private void runPachubeConsole(){
		PachubeRepositoryHandler phandler = new PachubeRepositoryHandler();
		phandler.init();
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		boolean keepRunning = false;
		
		try {
			String startup = br.readLine();
			if(startup.equals("run")){
				keepRunning = true;
				phandler.start();
			}
			else if(startup.equals("output")){
				phandler.serializeRepo();
			}
			else if(startup.equals("print")){
				phandler.serializeRepo(true);
			}
			
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		
		while(keepRunning){
			try {
				String str = br.readLine();
				if (str.equals("reposize")){
					System.out.println(phandler.getRepoSize() + " Statements in Repository.");
				}
				else if (str.equals("quit")){
					phandler.shutDown();
					keepRunning = false;
				}
				else if (str.startsWith("add")){
					String[] args = str.split(" ");
					if(args == null || args.length != 3){
						System.out.println("No valid add statement!");
					}
					else{
						phandler.addDatastream(args[1], args[2]);
					}
				}
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public EventApp(){
		log.setLevel(Level.ERROR);
		appender = new ConsoleAppender(new PatternLayout("%-4r [%t] %-5p %c %x - %m%n"));
		log.addAppender(appender);
	}
	
	private void initTwitterCrawl() {
		repoHandler = new TwitterRepositoryHandler();
		repoHandler.init();
		twitterHandler = new TwitterHandler(keywords);
		twitterHandler.init(repoHandler);
	}
	
	private void runTwitterCrawl() {
		twitterHandler.startStreaming();
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		while(true){
			try {
				String str = br.readLine();
				if (str.equals("reposize")){
					System.out.println(repoHandler.getRepoSize() + " Statements in Repository.");
				}
				else if (str.equals("")){
					repoHandler.toggleShowStream();
				}
				else if(str.equals("output")){
					repoHandler.serializeRepo();
				}
				else if (str.equals("quit")){
					System.out.println("quitting...");
					twitterHandler.stopStreaming();
					System.out.println("Twitter shut down");
					repoHandler.shutDown();
					System.out.println("Shutdown complete");
					System.exit(0);
				}
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
